home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr40 / x1j4_src.zip / MISC.C < prev    next >
Text File  |  1995-02-04  |  11KB  |  405 lines

  1. /*****************************************************************************/
  2. /*                                              */
  3. /*                                         */
  4. /*    *****              *****                      */
  5. /*     *****            *****                         */
  6. /*       *****          *****                         */
  7. /*         *****        *****                         */
  8. /*  ***************      ***************                     */
  9. /*  *****************    *****************                     */
  10. /*  ***************      ***************                     */
  11. /*         *****        *****       TheNet                    */
  12. /*       *****          *****       Portable. Compatible.         */
  13. /*     *****            *****       Public Domain             */
  14. /*    *****              *****    NORD><LINK                  */
  15. /*                                         */
  16. /* This software is public domain ONLY for non commercial use                */
  17. /*                                                                           */
  18. /*                                         */
  19. /*                                                                          */
  20. /*****************************************************************************/
  21.  
  22. /* Miscellaneous extras
  23.  * G8KBB April 1991 
  24.  *
  25.  * September 1993 - released as TheNet X-1J
  26.  *
  27.  * put arp_service back in iproute.c
  28.  * add support for l3 heard list in flush routines
  29.  * add port flush routine
  30.  *
  31.  * Released as TheNet X-1J release 4, January 1995
  32.  */
  33.  
  34. #include "all.h"
  35. #include "tntyp.h"        /* Definition der Typen                 */
  36.  
  37. #ifdef BANKED
  38. #define EXTERN extern
  39. #else
  40. #define EXTERN
  41. #endif
  42.  
  43. #include "miscv.h"
  44. #include "misce.h"        /* Externe Definitionen                 */
  45.  
  46. #ifdef MANAGED
  47. #ifdef BANKED
  48. extern char L4audit[];
  49. /* extern char L1audit[];
  50. extern char CPUaudit[];
  51. extern char BUFMSG[];
  52. extern char CPUMSG[];
  53. extern char TXMSG[];
  54. extern char RXMSG[]; */
  55. #else
  56. char L4audit[] = "L4    `";
  57. #endif
  58. char L1audit[] = "L1    `";
  59. char CPUaudit[] = "CPU   `";
  60. char BUFMSG[] = "BUF";
  61. char CPUMSG[] = "CPU";
  62. char TXMSG[] = "%Tx";
  63. char RXMSG[] = "%Rx";
  64. #endif
  65.  
  66. /*---------------------------------------------------------------------------*/
  67. VOID    initmisc()            /* Level 4 initialisieren         */
  68. {
  69.   register unsigned cnt;        /* Sratch Zaehler             */
  70. #ifdef STATSCMD
  71.   register unsigned i;
  72.   register char *ptr;
  73. #endif
  74.  
  75.     if (! iswarm())            /* im Kaltstart Standardwerte         */
  76.     {
  77.  
  78. #ifdef CWID
  79.     cwiden = DEFCW;            /* set target counter in seconds */
  80.     cwidtm = 0;            /* Start off counting from zero */
  81.     cwspeed = DEFCWS;        /* set dot time ( 10's of msecs ) */
  82.     cwstate = 3;            /* Set state to 'finished' */
  83.     cwtimr = 0;            /* disable cwid interrupt process */
  84. #endif
  85.  
  86. #ifdef STATSCMD
  87.  
  88.     /* clear the level 1 stats tables 
  89.      */
  90.     for( cnt = 0; cnt < NUM1STATS; cnt++ )
  91.         minbuf[cnt] = cpucnt[cnt] = txstat[cnt] = rxstat[cnt] = 0;
  92.  
  93.     /* now clear other misc stats counters
  94.      */
  95.     txon = dcdon = usetim = 0;
  96.     cpu2load = cpuload = 0;
  97.     minfre = 0x7fff;
  98.     usehour = 0;
  99. #endif
  100.  
  101. #ifdef MONITORCMD
  102.     runtime = 0;            /* reset the runtime counter */
  103. #ifdef L3MONITOR
  104.     l3mhlcnt =
  105. #endif
  106.     mhlcount = DEFMHL;        /* set default heard list length */
  107. #endif
  108.  
  109.     }
  110.  
  111. #ifdef MONITORCMD
  112.     inithd( &heardl );            /* clear the heard list */
  113.     uptime = heardtm = seconds = 0;        /* reset the time counters */
  114. #ifdef L3MONITOR
  115.     inithd( &l3heardl );            /* clear L3 heard list */
  116. #endif
  117. #endif
  118. #ifdef STATSCMD
  119.     ptr = l4stats;
  120.     for( i = sizeof( l4stats); i !=0; i-- )
  121.         *ptr++ = '\0';
  122. #endif
  123. #ifdef MOD_MTU
  124.     mtu_i_max = DEF_MTU_I_MAX;
  125.     mtu_l2_max = DEF_MTU_L2_MAX;
  126.     mtu_ip0 = DEF_MTU_IP0;
  127.     mtu_ip1 = DEF_MTU_IP1;
  128.     mtu_ipn = DEF_MTU_IPN;
  129. #endif
  130. }
  131.  
  132. /*---------------------------------------------------------------------------*/
  133. VOID    miscsrv()        /* Miscellaneous timer service             */
  134. {
  135.  
  136. #ifdef CWID
  137.     cwid();        /* call the cwid timer program each second */
  138. #endif
  139.  
  140. #ifdef MONITORCMD
  141.     seconds++;    /* bump the seconds counter */
  142.     flushheard();    /* remove old entries in the heard list hourly */
  143. #endif
  144.  
  145. #ifdef STATSCMD
  146.     chuse();    /* every 10 minutes, update level 1 stats info */
  147. #endif
  148.  
  149. }
  150.  
  151. /*-------------------------------------------------------------------------
  152.  * CWID keyer - G8KBB
  153.  * Daemon that every n minutes, sends the station ID in CW 
  154.  */
  155.  
  156. #ifdef CWID
  157.  
  158. /* This function is the main daemon that is called every second.
  159.  * If beaconing enabled, and if timeout has occurred, then call
  160.  * cwsend();
  161.  */
  162.  
  163. VOID cwid()
  164. {
  165.     if( cwiden != 0 )        /* If enabled */
  166.     {
  167.         if(++cwidtm >= cwiden)    /* bump counter & check timeout */
  168.         {            /* if timeout period passed, */
  169.             cwidtm = 0;    /* reset counter */
  170.             if( cwstate == 3 )    /* check not already sending */
  171.                 cwsend();    /* and send callsign in cw */
  172.         }
  173.         else if( cwstate == -1 && cwidtm > 60 ) /* If not sent 1 min */
  174.             kicktx( HDLCPORT );    /* later then force it   */
  175.     }
  176. }
  177.  
  178. /* Table of mappings from character to dots & dashes.
  179.  * For each entry, the top 3 bits are the number of elements
  180.  * in the character, and the lower 5 bits are the dots & dashes.
  181.  * The bits are used starting with bit 0, and a 1 represents a dash.
  182.  */
  183.  
  184. unsigned char cwchar[] =
  185. {
  186.     0x42, 0x81, 0x85, 0x61, 0x20, 0x84, 0x63, 0x80, 0x40, 0x8e, /* a-j */
  187.     0x65, 0x82, 0x43, 0x41, 0x67, 0x86, 0x8b, 0x62, 0x60, 0x21, /* k-t */
  188.     0x64, 0x88, 0x66, 0x89, 0x8d, 0x83,                /* u-z */
  189.     0xbf, 0xbe, 0xbc, 0xb8, 0xb0, 0xa0, 0xa1, 0xa3, 0xa7, 0xaf, /* 0-9 */
  190.     0x0
  191. };
  192.  
  193. /* Routine called by the daemon to send the callsign in CW
  194.  */
  195.  
  196. cwsend()
  197. {
  198.     register int i,c;
  199.  
  200.     for( i = 0; i < CWIDLEN && DEFCAL[i] != ' '; i++ )
  201.     {
  202.         c = upcase( DEFCAL[i] );
  203.         if( (c >= 'A') && (c <= 'Z') )
  204.             c -= 'A';
  205.         else if( (c >= '0') && (c <= '9') )
  206.             c += 'Z' - '0' - 'A' + 1;
  207.         else
  208.             c = '9' + 'Z' - '0' + 2 - 'A';
  209.         cwdat[i] = cwchar[c];
  210.     }
  211.     cwdat[i] = 0;        /* NULL signifies the end of the callsign */
  212.     cwbit = (*cwdat >> 5)&7; /* set bits counter to first character */
  213.     cwch  = *cwdat & 0x1f;    /* set first character */
  214.     cwofs = 0;        /* point to first character */
  215.     cwstate = -1;        /* set to start state */
  216. }
  217.  
  218. #endif /* CWID */
  219.  
  220.  
  221. #ifdef MONITORCMD
  222.  
  223. /* called each second, this program purges the heard list each hour.
  224.  * All entries not heard for 12 hours are removed.
  225.  */
  226.  
  227. #ifdef L3MONITOR
  228. flushheard()
  229. {
  230.     if(++heardtm >= 3600)        /* bump counter & check timeout */
  231.     {                /* if timeout period passed, */
  232.         heardtm = 0;        /* reset counter */
  233.         flush_mhlist( &heardl, mhlcount );
  234.         flush_mhlist( &l3heardl, l3mhlcnt );
  235.         runtime++;        /* bump timers */
  236.         uptime++;
  237.     }
  238. }
  239.  
  240. flush_mhlist( list, count)
  241. MHEARD *list;
  242. unsigned char count;
  243. {
  244.     register MHEARD *mhp, *mhp2;
  245.     register unsigned i;
  246.  
  247.     mhp = list->nexthb;
  248.     i = 0;
  249.     while( mhp != list )    /* step through the list */
  250.     {
  251.         mhp2 = mhp->nexthb;        /* copy the pointer  */
  252.         if( seconds - mhp->lastheard > 3600*12  /* too old? */
  253.             || i > count )        /* list too long ? */
  254.             dealoc(unlink( mhp ));    /* kill this entry */
  255.         mhp = mhp2;            /* and continue      */
  256.         i++;
  257.     }
  258. }
  259.  
  260. #else    /* L3MONITOR */
  261.  
  262. flushheard()
  263. {
  264.     register MHEARD *mhp, *mhp2;
  265.  
  266.     if(++heardtm >= 3600)    /* bump counter & check timeout */
  267.     {            /* if timeout period passed, */
  268.         heardtm = 0;    /* reset counter */
  269.         mhp = heardl.lnext;
  270.         while( mhp != &heardl )    /* step through the list */
  271.         {
  272.             mhp2 = mhp->nexthb;        /* copy the pointer  */
  273.             if( seconds - mhp->lastheard > 3600*12 ) /* too old? */
  274.                 dealoc(unlink( mhp ));    /* kill this entry */
  275.             mhp = mhp2;            /* and continue      */
  276.         }
  277.         runtime++;                /* bump timers */
  278.         uptime++;
  279.     }
  280. }
  281.  
  282. #endif    /* L3MONITOR */
  283. #endif    /* MONITORCMD */
  284.  
  285. /*--------------------------------------------------------------------------
  286.  * STATS command daemon. - G8KBB
  287.  * Every 10 minutes, shift the contents of the 10 minute summaries, losing
  288.  * the oldest. The current entries are then formed by reading the totals
  289.  * of the 10 ms updated counters, dividing by 600 to give percentages
  290.  * and clearing the totalisers.
  291.  */
  292.  
  293. #ifdef STATSCMD
  294. VOID chuse()
  295. {
  296.     register int i,j;
  297.  
  298.     if( ++usetim >= 600 )            /* every 10 minutes */
  299.     {
  300.         usetim = 0;            /* reset 10 minute counter */
  301.         for( i=NUM1STATS-1; i>0; i-- )    /* this loop copies the */
  302.         {                /* entries in the table */
  303.             txstat[i]=txstat[i-1];    /* losing the oldest one */
  304.             rxstat[i]=rxstat[i-1];
  305.             cpucnt[i]=cpucnt[i-1];
  306.             minbuf[i]=minbuf[i-1];
  307.         }
  308. #ifdef PORTFLUSH
  309.         flush_check();        /* check for port block flush needed */
  310. #endif
  311.         if( ++usehour >= 6 )
  312.         {
  313.             usehour = 0;
  314.             for( i=NUMPORTS*2-1; i >= NUMPORTS; i-- )
  315.                 for( j = 0; j < NUML2STATS; j++ )
  316.                     l2stats[j][i] = l2stats[j][i-2];
  317.             for( i = 0; i < NUMPORTS; i++ )
  318.                 for( j = 0; j < NUML2STATS; j++ )
  319.                     l2stats[j][i] = 0;
  320.             for( i = 0; i < NUML4STATS; i++ )
  321.             {
  322.                 l4stats[i][1] = l4stats[i][0];
  323.                 l4stats[i][0] = 0;
  324.             }
  325.             l3gwcnt[1] = l3gwcnt[0];
  326.             l3gwcnt[0] = 0;
  327.         }
  328.         txstat[0] = txon / 600;        /* set the most recent entry */
  329.         rxstat[0] = dcdon / 600;    /* from the counters */
  330.         cpucnt[0] = cpuload;
  331.         minbuf[0] = minfre;
  332. #ifdef MANAGED
  333.         if( auditmask & 1 )
  334.         {
  335.             notify( L1audit, 0x86, txstat[0], NULL, TXMSG, 3 );
  336.             notify( L1audit, 0x86, rxstat[0], NULL, RXMSG, 3 );
  337.         }
  338.         if( auditmask & 0x40 )
  339.         {
  340.             notify( CPUaudit, 0x86, cpuload, NULL, CPUMSG, 3 );
  341.             notify( CPUaudit, 0x86, minfre, NULL, BUFMSG, 3 );
  342.         }
  343. #endif
  344.         cpuload = txon = dcdon = 0;        /* and reset the counters */
  345.         minfre = 0x7fff;
  346.     }
  347. }
  348.  
  349. #endif
  350.  
  351. /**************************************************************************\
  352. *                                                                          *
  353. * "flush_check"                                                            *
  354. *                                                                          *
  355. * Check to see if a port is blocked. If so, then flush pending data        *
  356. * if nothing has been sent when data is pending.                           *
  357. *                                                                          *
  358. \**************************************************************************/
  359.  
  360. #ifdef PORTFLUSH
  361.  
  362. flush_check()
  363. {
  364.     register unsigned port;
  365.     
  366.     for( port = 0; port < NUMPORTS; port++ )
  367.     {
  368.         DIinc();
  369.         if( pending[port] && !sentdata[port] )
  370.         {
  371.             if( none_last_time[port] )
  372.             {
  373.                 l1get( 0x8001 | ( port << 8 ) );
  374.                 flushed[port]++;
  375.                 pending[port] = 0;
  376.             }
  377.             none_last_time[port] = 1;
  378.         }
  379.         else
  380.             none_last_time[port] = 0;
  381.         if( sentdata[port] )
  382.             pending[port] = 0;
  383.         sentdata[port] = 0;
  384.         decEI();
  385.     }
  386. }
  387.  
  388. #endif
  389.  
  390. /**************************************************************************\
  391. *                                                                          *
  392. * "uppercase"                                                              *
  393. *                                                                          *
  394. * Zeichen c in Grossbuchstabe umwandeln, wenn Kleinbuchstabe, und          *
  395. * zurueckgeben.                                                            *
  396. *                                                                          *
  397. \**************************************************************************/
  398.  
  399. upcase(c)
  400. register char c;
  401.   {
  402.     return ( ('a' > c || 'z' < c) ? c : c - ('a' - 'A') );
  403.   }
  404.  
  405.